package com.zfx.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zfx.pojo.Address;
import com.zfx.service.IAddressService;
import com.zfx.util.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @description:
 * @author: zfx
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;


    @RequestMapping("/list")
    public JsonObject getList(HttpServletRequest request, @RequestBody String jsonData) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();
        Map<String, Object> dataMap = objectMapper.readValue(jsonData, HashMap.class);

        Address address = new Address(dataMap);
        address.setUserId((Integer) request.getSession().getAttribute("userId"));

        List<Address> addressList = addressService.findByAddress(address, (int) dataMap.get("page"), (int) dataMap.get("limit"));

        jsonObject.setData("addressList", addressList);
        jsonObject.setDataSize(addressService.countAll(address));
        jsonObject.setCode("200");
        jsonObject.setMsg("success");

        return jsonObject;
    }

    @RequestMapping("/getInfo")
    public JsonObject getInfo(HttpServletRequest request, @RequestBody Address record) throws IOException {
        JsonObject jsonObject = new JsonObject();

        record.setUserId((Integer) request.getSession().getAttribute("userId"));
        Address address = addressService.findByUserIdAndHaoyouId(record);

        if(address != null) {
            jsonObject.setData("address", address);
            jsonObject.setCode("200");
            jsonObject.setMsg("success");
        } else {
            jsonObject.setCode("500");
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/update")
    public JsonObject update(HttpServletRequest request, @RequestBody Address address) throws IOException {
        JsonObject jsonObject = new JsonObject();

        address.setUserId((int) request.getSession().getAttribute("userId"));
        int updateNum = addressService.update(address);

        if(updateNum == 1) {
            jsonObject.setMsg("success");
        } else {
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/deleteOne")
    public JsonObject deleteOne(HttpServletRequest request, @RequestBody Address address) {
        JsonObject jsonObject = new JsonObject();
        address.setUserId((int) request.getSession().getAttribute("userId"));

        int deleteNum = addressService.delete(address);

        address.setUserId(address.getHaoyouId());
        address.setHaoyouId((int) request.getSession().getAttribute("userId"));

        deleteNum += addressService.delete(address); //双向删除

        if(deleteNum == 2) {
            jsonObject.setMsg("success");
        } else {
            jsonObject.setMsg("error");
        }
        return jsonObject;
    }

    @RequestMapping("/deleteList")
    public JsonObject deleteList(HttpServletRequest request, @RequestBody String sFriendId) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, Object> jsonMap = objectMapper.readValue(sFriendId, HashMap.class);

        Integer userId = (Integer) request.getSession().getAttribute("userId");

        String sfriendList = String.valueOf(jsonMap.get("haoyouIdArr"));
        List<Integer> friendIdList = objectMapper.readValue(sfriendList, ArrayList.class);

        if(userId != null) {
            Address address = new Address();
            for (Integer friendId : friendIdList) {
                address.setUserId(userId);
                address.setHaoyouId(friendId);
                addressService.delete(address);
                address.setUserId(friendId);
                address.setHaoyouId(userId);
                addressService.delete(address); //双向删除
            }
            jsonObject.setMsg("success");
        } else {
            jsonObject.setMsg("error");
        }

        return jsonObject;
    }
}
